<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
import { getMenuList } from "@/api/menuApi.js";
import { showMessage } from "@/utils/msg.js";

export default {
  name: "App",
  provide() {
    return {
      reload: this.reload
    };
  },
  data() {
    return {
      isRouterAlive: true
    };
  },
  created() {
    showMessage();
    this.initState();
    this.getMenuList();
    let sessionStore = sessionStorage.getItem("user_store");
    sessionStore && this.$store.replaceState(JSON.parse(sessionStore));
  },
  methods: {
    // 初始化State
    initState() {
      this.$store.dispatch("user/initState");
      this.$store.dispatch("worktab/initState");
      this.$store.dispatch("setting/initState");
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      });
    },
    // 获取菜单列表
    getMenuList() {
      getMenuList();
    }
  },
  mounted() {
    window.onbeforeunload = e => {
      sessionStorage.setItem("user_store", JSON.stringify(this.$store.state));
    };
  }
};
</script>
<style lang="" scope>
  html,body,#app{
    width: 100%;
    height: 100%;
  }
</style>